//
//  ZCBar.m
//  uplife
//
//  Created by ZhangCheng on 2018/12/25.
//  Copyright © 2018年 ZhangCheng. All rights reserved.
//

#import "ZCBar.h"
#define RGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
@interface ZCBar ()

@property (nonatomic) CATextLayer* textLayer;//标题Layer

@property (nonatomic,strong)NSMutableArray *barArr;//柱子上面进度数据

@property (nonatomic,assign)float totalNum;//总的进度时间

@property (nonatomic,strong)UILabel *titleLab;//下面标题  也可以使用CATextLayer

@end

@implementation ZCBar

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        
        
    }
    return self;
}
-(void)setTitleStr:(NSString *)titleStr
{
    _titleStr = titleStr;
    [self addSubview:self.titleLab];
    
    
    //CABasicAnimation *fade = [self fadeAnimation];
    //[self.textLayer addAnimation:fade forKey:nil];
    //[self.layer addSublayer:self.textLayer];
    
}

-(void)setDataArr:(NSMutableArray *)dataArr
{
    _dataArr = dataArr;
    
    self.barArr = [self barProgressArr:_dataArr];
    
//    [self setProgress];
    
}

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    __block float a = 0;
    //遍历数组
    [self.barArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        //画路径
        UIBezierPath *beziPath = [UIBezierPath bezierPath];
        beziPath.lineWidth = 0.0;//线的宽度
        //起点坐标
        
        [beziPath moveToPoint:CGPointMake(15,self.bounds.size.height-20)];
        //终点坐标
        [beziPath addLineToPoint:CGPointMake(15,(MAX_TIME-self.totalNum)/MAX_TIME*(self.bounds.size.height-20))];
        beziPath.lineCapStyle = kCGLineCapRound;
        beziPath.lineJoinStyle = kCGLineJoinRound;
        [beziPath stroke];
        
        //画图
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.fillColor = [UIColor clearColor].CGColor;
        shapeLayer.strokeColor = [obj[@"strokeColor"]CGColor];
        shapeLayer.lineWidth = CHART_BAR_WIDTH;//宽度 目前是固定宽度 可以根据情况进行修改
        shapeLayer.strokeStart = a;//起始点
        shapeLayer.strokeEnd = [obj[@"precent"] floatValue]+a;//结束点
        a = shapeLayer.strokeEnd;//从上一个起始点开始画图
        shapeLayer.path = beziPath.CGPath;//产生联系
        
        [self.layer addSublayer:shapeLayer];
        
        
        //动画
        CABasicAnimation*pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        pathAnimation.duration=1.0f;
        pathAnimation.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        pathAnimation.fromValue=@0.0f;
        pathAnimation.toValue=@(1);
        [shapeLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
        
        
    }];
    
    
}

//#pragma mark ---画图
//-(void)setProgress
//{
//    __block float a = 0;
//    //遍历数组
//    [self.barArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//
//        //画路径
//        UIBezierPath *beziPath = [UIBezierPath bezierPath];
//        beziPath.lineWidth = 0.0;//线的宽度
//        //起点坐标
//
//        [beziPath moveToPoint:CGPointMake(15,self.bounds.size.height-20)];
//        //终点坐标
//        [beziPath addLineToPoint:CGPointMake(15,(MAX_TIME-self.totalNum)/MAX_TIME*(self.bounds.size.height-20))];
//        beziPath.lineCapStyle = kCGLineCapRound;
//        beziPath.lineJoinStyle = kCGLineJoinRound;
//        [beziPath stroke];
//
//        //画图
//        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
//        shapeLayer.fillColor = [UIColor clearColor].CGColor;
//        shapeLayer.strokeColor = [obj[@"strokeColor"]CGColor];
//        shapeLayer.lineWidth = 30.0;//宽度 目前是固定宽度 可以根据情况进行修改
//        shapeLayer.strokeStart = a;//起始点
//        shapeLayer.strokeEnd = [obj[@"precent"] floatValue]+a;//结束点
//        a = shapeLayer.strokeEnd;//从上一个起始点开始画图
//        shapeLayer.path = beziPath.CGPath;//产生联系
//
//        [self.layer addSublayer:shapeLayer];
//
//
//        //动画
//        CABasicAnimation*pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
//        pathAnimation.duration=1.0f;
//        pathAnimation.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//        pathAnimation.fromValue=@0.0f;
//        pathAnimation.toValue=@(1);
//        [shapeLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
//
//
//    }];
//
//
//
//}
#pragma mark ---处理进度数据
-(NSMutableArray *)barProgressArr:(NSMutableArray *)dataArr
{
    NSMutableArray *barProgArr = [[NSMutableArray alloc]initWithCapacity:0];
    
    self.totalNum = 0.0;//总的时间
    float value = 0.0;//每一段时间
    NSString *boolStr;
    //计算总时间
    for (NSDictionary *dic in dataArr)
    {
        value = [dic[@"precent"] floatValue];
        
        self.totalNum = self.totalNum + value;
    }
    
    //计算进度值
    for (NSDictionary *dic in dataArr)
    {
        value = [dic[@"precent"] floatValue];
        
        boolStr = dic[@"strokeColor"];
        
        NSMutableDictionary *progDic = [[NSMutableDictionary alloc]initWithCapacity:0];
        
        //根据BOOL确定颜色  当然也可以添加多种颜色
        if ([boolStr isEqualToString:@"1"])
        {
            [progDic setObject:[UIColor colorWithRed:126/255.0 green:214/255.0 blue:219/255.0 alpha:1.0] forKey:@"strokeColor"];
        }
        else
        {
            [progDic setObject:[UIColor colorWithRed:244/255.0 green:201/255.0 blue:108/255.0 alpha:1.0] forKey:@"strokeColor"];
        }
        
        //计算进度 添加进数组
        [progDic setObject:@(value/self.totalNum) forKey:@"precent"];
        
        [barProgArr addObject:progDic];
        
    }
    
    return barProgArr;
}
-(UILabel *)titleLab
{
    if (!_titleLab)
    {
        _titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, self.bounds.size.height-20, self.bounds.size.width+1.0f, 20)];
        _titleLab.font = [UIFont systemFontOfSize:13.0];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.textColor = [UIColor colorWithRed:28/255.0 green:32/255.0 blue:41/255.0 alpha:1.0];
        _titleLab.text = self.titleStr;
        
    }
    return _titleLab;
}
#pragma mark ---可以使用CATextLayer
-(CATextLayer*)textLayer
{
    if (!_textLayer) {
        _textLayer = [[CATextLayer alloc]init];
        _textLayer.string = self.titleStr;
        _textLayer.frame = CGRectMake(0, self.bounds.size.height-20, self.bounds.size.width, 20);
        [_textLayer setAlignmentMode:kCAAlignmentCenter];
        [_textLayer setForegroundColor:[[UIColor colorWithRed:28/255.0 green:32/255.0 blue:41/255.0 alpha:1.0] CGColor]];
        
        _textLayer.fontSize = 14.0;
    }
    
    return _textLayer;
}
//动画
-(CABasicAnimation*)fadeAnimation
{
    CABasicAnimation* fadeAnimation = nil;
    fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fadeAnimation.fromValue = [NSNumber numberWithFloat:0.0];
    fadeAnimation.toValue = [NSNumber numberWithFloat:1.0];
    fadeAnimation.duration = 2.0;
    return fadeAnimation;
}
@end
